﻿// onwer 蒋家伟
#pragma once
// qt
#include <QObject>

// jsdk
#include "ISerialPort.h"
#include "ThreadDeleter.h"
#include "SerialPortWorker.h"

class QThread;
namespace jsdk
{
    class SerialPort final: public QObject, public ISerialPort
    {
        Q_OBJECT
    public:
        static OwnerPtr<SerialPort> create(ISerialPortEventHandler * pSerialPortEventHandler);
        ~SerialPort() {
        }

    private:
        // 继承ISocket
        virtual void connected(
            const std::string& portName,
            const int baudRate,
            const short dataBits,
            const short stopBits,
            const short parity,
            const short flowControl) override;

        virtual void reconnect(
            const std::string& portName,
            const int baudRate,
            const short dataBits,
            const short stopBits,
            const short parity,
            const short flowControl
            ) const override;

        virtual void send(std::string&& msg, int64_t length) const override;
        virtual bool isOnLine() const override;

        // owner,用于内部初始化信号与槽
        bool init() const;
        SerialPort();

    signals:
        void sendMessage(QByteArray, qint64) const;
        void connectSerialPort(
            QString port,
            int baudRate,
            short dataBits,
            short stopBits,
            short parity,
            short flowControl
            ) const;

    private:
        OwnerPtr<QThread, ThreadDeleter> m_opThread;
        OwnerPtr<SerialPortWorker> m_opSerialPortWorker;
    };
}